package com.project.srtp.common.result;

import com.project.srtp.common.result.errorcode.ResultCode;

/**
 * 全局返回对象构造器（工厂类）
 */
public class Results {

    // 成功构造方法
    public static <T> Result<T> success(T data) {
        Long currentTimeMillis = System.currentTimeMillis();
        return new Result<T>()
                .setData(data)
                .setCode(ResultCode.SUCCESS.code())
                .setMessage(ResultCode.SUCCESS.message())
                .setTimestamp(currentTimeMillis);
    }

    // 成功构造方法（空参）
    public static Result<Void> success() {
        Long currentTimeMillis = System.currentTimeMillis();
        return new Result<Void>()
                .setCode(ResultCode.SUCCESS.code())
                .setMessage(ResultCode.SUCCESS.message())
                .setTimestamp(currentTimeMillis);
    }


    // 错误构造方法
    public static <T> Result<T> error(ResultCode code) {
        Long currentTimeMillis = System.currentTimeMillis();
        return new Result<T>()
                .setCode(code.code())
                .setMessage(code.message())
                .setTimestamp(currentTimeMillis);
    }

    public static <T> Result<T> error(ResultCode code, String customMessage) {
        Long currentTimeMillis = System.currentTimeMillis();
        return new Result<T>()
                .setCode(code.code())
                .setMessage(customMessage)
                .setTimestamp(currentTimeMillis);

    }

    // 自定义构造方法
    public static <T> Result<T> of(int code, String message, T data) {
        return new Result<T>()
                .setCode(code)
                .setMessage(message)
                .setData(data)
                .setTimestamp(System.currentTimeMillis());
    }
}